package com.qububu.common.notice.service.impl;

import java.util.List;

import javax.annotation.Resource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.Element;

import org.springframework.stereotype.Service;

import com.qububu.common.notice.dto.NoticeInfoDto;
import com.qububu.common.notice.service.NoticeService;
import com.qububu.db.dao.NoticeDao;
import com.qububu.systemBase.exceptions.NotFindCartException;

@Service
public class NoticeServiceImpl implements NoticeService {

	@Resource
	private NoticeDao noticeDao;
	
	@Resource
	private Cache noticeCache;
	

	public NoticeInfoDto getNoticeInfo(int realNoticeId) {
		return noticeDao.getNoticeInfo(realNoticeId);
	}
	
	public NoticeInfoDto doModifyNotice(NoticeInfoDto noticeInfoDto) {
		noticeDao.doModifyNotice(noticeInfoDto);
		
		return noticeInfoDto;
	}

	public void doDeleteNoticeById(int realNoticeId) {
		noticeDao.doDeleteNoticeById(realNoticeId);
	}

	public NoticeInfoDto doAddNoticeInfo(NoticeInfoDto noticeInfoDto) {
		noticeDao.doAddNoticeInfo(noticeInfoDto);
		return noticeInfoDto;
	}
	
	/**
	 * 将notice列表信息存入缓存中
	 * @param value 需要存入缓存的List
	 * */
	public void doSaveMainNoticeListToCache(String noticeMessageKey, List<NoticeInfoDto> noticeInfoList){
		
		Element noticeElement = noticeCache.get(noticeMessageKey);
		
		// 取出缓存中存放List
		if (noticeElement == null) {
			noticeCache.put(new Element(noticeMessageKey, noticeInfoList));
		}
	}
	
	/**
	 * 根据key找出缓存中存放List
	 * @param noticemessagekey 存入的notice信息
	 * @throws NotFindCartException 
	 * */
	public List<NoticeInfoDto> getMainNoticeListFromCache(String noticeMessageKey){
		List<NoticeInfoDto> noticeInfoList = null;
		
		Element noticeElement = noticeCache.get(noticeMessageKey);
		
		// 取出缓存中存放List
		if (noticeElement != null) {
			noticeInfoList = (List<NoticeInfoDto>) noticeElement.getObjectValue();
//			System.out.println("\n\n------从--- [ 缓存 ]---中取出公告信息列表------------\n\n");
		} 
		
		return noticeInfoList;
	}

	public List<NoticeInfoDto> getMainSiteNoticeList() {
		List<NoticeInfoDto> siteNoticeList = noticeDao.getMainSiteNoticeList();
//		System.out.println("\n\n------从 [数据库] 中取出--[ 系统 ]---公告信息列表------------\n\n");
		return siteNoticeList;
	}

	public List<NoticeInfoDto> getMainCommunityNoticeList() {
		List<NoticeInfoDto> communityNoticeList = noticeDao.getMainCommunityNoticeList();
//		System.out.println("\n\n------从 [数据库] 中取出--[ 社区 ]---公告信息列表-----------\n\n");
		return communityNoticeList;
	}

	public List<NoticeInfoDto> getAllNotice() {
		return noticeDao.getAllNoticeList();
	}

}
